package com.wynn.service.impl;

import com.wynn.dao.ProductDao;
import com.wynn.pojo.Bom;
import com.wynn.pojo.Product;
import com.wynn.pojo.ProductLine;
import com.wynn.pojo.Result;
import com.wynn.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Transactional
@RequestMapping("product")
public class ProductServiceImpl implements ProductService {

    @Autowired
    private ProductDao productDao;
    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    @GetMapping("selectAll")
    public Result selectAll() {
        Result result = new Result();
        List<Product> productList = null;
        Boolean flag = redisTemplate.hasKey("productList" );
        if (flag) {
            // 优先从缓存获取
            productList = redisTemplate.opsForList().range("productList", 0, -1);
        }else {
            productList = productDao.findAllProduct();
            redisTemplate.opsForList().rightPushAll("productList", productList);
        }
        result.setData(productList);
        return result;
    }

    @Override
    @PostMapping("add")
    public void add(@RequestBody Product product) throws Exception {
        redisTemplate.delete("productList");
        Product exitProduct = productDao.selectByModel(product);
        if (exitProduct!=null){
            throw new Exception();
        }
        int i = productDao.add(product);
        if (i!=1){
            throw new Exception();
        }
    }

    @Override
    @GetMapping("delete")
    public void delete(int id) throws Exception {
        redisTemplate.delete("productList");
        int i = productDao.delete(id);
        if(i!=1){
            throw new Exception();
        }
    }

    @Override
    @PostMapping("edit")
    public void edit(@RequestBody Product product) throws Exception {
        redisTemplate.delete("productList");
        int i = productDao.edit(product);
        if (i!=1){
            throw new Exception();
        }
    }

    @Override
    @GetMapping("search")
    public Result search(String productModel) {
        Result result = new Result();
        List<Product> productList = productDao.search(productModel);
        result.setData(productList);
        return result;
    }

    @Override
    @GetMapping("bom")
    public List<Bom> bom() {
        //搜寻所有根节点为0的子集合
        List<Bom> boms = this.recursionQuery(0);
        return boms;
    }

    //递归搜寻子节点
    private List<Bom> recursionQuery(int futherId){
        List<Bom> boms = productDao.findAllBomByFid(futherId);
        for (Bom bom : boms) {
            List<Bom> cbom = this.recursionQuery(bom.getId());
            bom.setChildren(cbom);
        }
        return boms;
    }

    @Override
    @PostMapping("bomAdd")
    public void bomAdd(@RequestBody Bom bom) {
        productDao.bomAdd(bom);
    }

    @Override
    @GetMapping("deleteBom")
    public void deleteBom(int id) {
        //递归删除子节点
        this.recursionDelete(id);
        productDao.deleteBom(id);
    }

    private void recursionDelete(int fatherId){
        List<Bom> allBom = productDao.findAllBomByFid(fatherId);
        for (Bom bom : allBom) {
            this.recursionDelete(bom.getId());
            productDao.deleteBom(bom.getId());
        }
    }

    @Override
    @PostMapping("editBom")
    public void editBom(@RequestBody Bom bom) {
        productDao.editBom(bom);
    }

    @Override
    @GetMapping("searchBom")
    public List<Bom> searchBom(String name) {
        List<Bom> rootBoms = productDao.findAllBomByName(name);

        for (Bom bom : rootBoms) {
            bom.setChildren(recursionQuery(bom.getId()));
        }
        return rootBoms;
    }


}
